﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WorkNodeFramework
{
    public enum WhereComparision { 
        Equals,
        NotEquals,
        Greater,
        Leaster,
        EqualsOrGreater,
        EqualsOrLeaster,
        In,
        NotIn,
        Between,
        IsNull,
        Like
    }

    public interface IWhereCondition{
        IWhereCondition[] Conditions{get;}
    }

    public class WhereCondition:IWhereCondition
    {
        Column col;
        object value;
        WhereComparision comparision = WhereComparision.Equals;

        public WhereCondition(Column col) {
            this.col = col;
        }

        public Column Column {
            get {
                return col;
            }
        }

        public object Value {
            get {
                return value;
            }
        }

        public WhereComparision Comparision {
            get {
                return comparision;
            }
        }

        List<IWhereCondition> conditions = new List<IWhereCondition>();
        public IWhereCondition[] Conditions {
            get {
                return conditions.ToArray();
            }
        }

        public WhereCondition IsEqualsTo(object value) {
            comparision = WhereComparision.Equals;
            this.value = value;
            return this;
        }

        public WhereCondition IsNotEquals(object value)
        {
            comparision = WhereComparision.NotEquals;
            this.value = value;
            return this;
        }

        public WhereCondition IsGreaterThan(object value)
        {
            comparision = WhereComparision.Greater;
            this.value = value;
            return this;
        }

        public WhereCondition IsEqualsOrGreaterThan(object value) {
            comparision = WhereComparision.EqualsOrGreater;
            this.value = value;
            return this;
        }

        public WhereCondition IsLeasterThan(object value) {
            comparision = WhereComparision.Leaster;
            this.value = value;
            return this;
        }

        public WhereCondition IsEqualsOrLeasterThan(object value) {
            comparision = WhereComparision.EqualsOrLeaster;
            this.value = value;
            return this;
        }
        
        public WhereCondition Between(object value1, object value2) {
            comparision = WhereComparision.Between;
            this.value = new object[] { value1,value2};
            return this;
        }

        public WhereCondition In(params object[] values) {
            comparision = WhereComparision.In;
            this.value = values;
            return this;
        }

        public WhereCondition In(Select select)
        {
            comparision = WhereComparision.In;
            this.value = select;
            return this;
        }

        public WhereCondition NotIn(params object[] values)
        {
            comparision = WhereComparision.NotIn;
            this.value = values;
            return this;
        }

        public WhereCondition NotIn(Select select)
        {
            comparision = WhereComparision.NotIn;
            this.value = select;
            return this;
        }

        public WhereCondition Like(object value) {
            comparision = WhereComparision.Like;
            this.value = value;
            return this;
        }

        public WhereCondition IsNull() {
            comparision = WhereComparision.IsNull;
            return this;
        }
    }

    public class AndWhereCondition : IWhereCondition {

        IWhereCondition[] conditions;
        public AndWhereCondition(params IWhereCondition[] conditions) {
            this.conditions = conditions;
        }

        public IWhereCondition[] Conditions {
            get {
                return conditions;
            }
        }
    }

    public class OrWhereCondition : IWhereCondition
    {
        IWhereCondition[] conditions;
        public OrWhereCondition(params IWhereCondition[] conditions) {
            this.conditions = conditions;
        }

        public IWhereCondition[] Conditions {
            get {
                return conditions;
            }
        }
            
    }

    public class NotWhereCondition : IWhereCondition
    {
        IWhereCondition[] conditions;
        public NotWhereCondition(IWhereCondition condition)
        {
            this.conditions = new IWhereCondition[1]{condition};
        }

        public IWhereCondition[] Conditions
        {
            get
            {
                return conditions;
            }
        }

    }

    public static class WHERE {
        public static WhereCondition DO(Column column) {
            return new WhereCondition(column);
        }
        public static IWhereCondition AND(params IWhereCondition[] conditions) {
            return new AndWhereCondition(conditions);
        }
        public static IWhereCondition OR(params IWhereCondition[] conditions) {
            return new OrWhereCondition(conditions);
        }

        public static IWhereCondition NOT(IWhereCondition condition)
        {
            return new NotWhereCondition(condition);
        }
    }
}
